<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use src\logic\lconfig;
class Cwithdraw extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		$view->assign('type', $dlang->get('earn_type'));
		$view->assign('checkstate', $dlang->get('sh0005'));
		$view->assign('paytype', $dlang->get('earn_pay'));
		$view->assign('status', $dlang->get('deposit_status'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/deposit.ext.js');
	}
	
	public function dotask() {
		$id = (int)input::get_post('s');
		if ($id < 1) {
			C\redirect('/admin');
		}
		$orm  = C\model::load('pro.pl.withdraw');
		$data = $orm->get_one($id);
		if (!$data) {
			C\redirect('/admin');
		}
		if ($data['executeid'] > 0) {
			$url = Cadmin::taskurl($data['executeid']);
			C\redirect($url);
		}
		//已经经理了一办的审核或者经过审核了 就不允许了
		if ($data['paystate'] != 0) {
			C\redirect('/admin');
		} 
		$user   = Cadmin::user($data['userid']);
		$args   = array('userid'=>$data['userid'], 'status'=>1);
		$args   = \classlib\request::querystr($args);
		$link   = C\router::get_url('admin/live/log/moneyflow').'?'.$args;
		self::__get_payment($data);
		$aorm   = C\model::load('pro.pl.anchor');
		$anchor = $aorm->get_one($data['userid']);
		$money  = $data['money'] / lconfig::CANRATIO;
		$vmoney = $anchor['lastcoins'] / lconfig::STARRATIO;
		$atime  = date('Y-m-d H:i:s', $data['addtime']);
		//创建流程执行实例 进行审核
		$notes  = '';
		if ($money > $vmoney) {
			$notes = '余额不足[Not enough balance]';
		}
		//DD审核状态
		$dlang  = C\lang::inst('dd');
		$ddtype = $dlang->get('sh0005');
		$ischeck= isset($ddtype[$data['accoutstate']])? $ddtype[$data['accoutstate']]:'UnKnow';
		//初始化的数据整合
		$item   = array('id'=>$data['id'], 'link'=>$link, 'ischeck'=>$ischeck,
			'sn'=>$data['sn'], 'payment'=>$data['payment'], 'money'=>$money);
		$earning= $anchor['earningcoins'] / lconfig::STARRATIO;
		$dataset= array('items'=>array($item), 'addtime'=>$atime, 'user'=>$user, 'notes'=>$notes,
			'vmoney'=>$vmoney, 'earning'=>$earning, 'salary'=>$anchor['salary']);
		$json = json_encode($dataset);
		$executeid = Cadmin::build_execute(Cadmin::WORKFLOW_DEPOSIT_ID, '0', $json);
		$orm->save($id, array('executeid'=>$executeid));
		$url = Cadmin::taskurl($executeid);
		C\redirect($url);
	}
	
	/**
	 * 更新 支付类型
	 * @param unknown $item
	 */
	private static function __get_payment(&$item) {
		$porm    = C\model::load('pro.pl.payment');
		$payment = $porm->get_one($item['userid']);
		if ($payment) {
			$item['accoutstate'] = 0;
			if ($item['paytype'] == 1) {
				$item['payment'] = sprintf('<font color="red">[Strip]</font>%s %s[%s]', $payment['first_name'],
						$payment['last_name'], $payment['bank_account']);
				$item['accoutstate'] = $payment['status'];
			} else if ($item['paytype'] == 2) {
				$item['payment'] = sprintf('<font color="red">[Paypal]</font>%s %s[%s]',
						$payment['firstname'], $payment['lastname'], $payment['paypal']);
				$item['accoutstate'] = 1;
			} else {
				$item['payment'] = '系统转换[Merge to Salary]';
			}
		} else {
			$item['payment']     = '未设置[UnSet]';
			$item['accoutstate'] = 0;
		}
	}
	
	
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$userid = input::post('userid');
		if (is_numeric($userid) && $userid > 0) {
			$where[] = array('userid', $userid, \classlib\orm::OP_EQ);
		}
		$paytype = input::post('paytype');
		if (is_numeric($paytype)) {
			$where[] = array('paytype', $paytype, \classlib\orm::OP_EQ);
		}
		$status = input::post('status');
		if (is_numeric($status)) {
			$where[] = array('status', $status, \classlib\orm::OP_EQ);
		}
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = array('stime', strtotime($sdate), \classlib\orm::OP_GT);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = array('stime', strtotime($edate), \classlib\orm::OP_LT);
		}
		$orm    = C\model::load('pro.pl.withdraw');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$aorm = C\model::load('pro.pl.anchor');
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['user']    = Cadmin::user($item['userid']);
				$item['stime']   = date('Y-m-d H:i:s', $item['stime']);
				$item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
				$item['money']   = $item['money'] / lconfig::CANRATIO;
				$item['paytype'] = intval($item['paytype']);
				$anchor = $aorm->get_one($item['userid']);
				$item['vmoney']  = $anchor['lastcoins'] / lconfig::STARRATIO;
				self::__get_payment($item);
				if ($item['executeid'] > 0) {
					$item['link']  = Cadmin::taskurl($item['executeid']);
				} else {
					$item['link']  = C\router::get_url('admin/live/finance/withdraw', 'dotask', array('s'=>$item['id']));
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}